<template>
  <el-dialog
    :title= title
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body>
    <div style="text-align: center;">
    <el-transfer
      style="text-align: left; display: inline-block "
      v-model="shopsList"
      :props="transferProps"
      filterable
      :filter-method="filterMethod"
      :titles="['未选', '已选']"
      :button-texts="['删除', '添加']"
      :format="{
        noChecked: '${total}',
        hasChecked: '${checked}/${total}'
      }"
      :data="allShopsList">
      <el-button class="transfer-footer" slot="right-footer" size="small" @click="dataFormSubmit">保存</el-button>
    </el-transfer>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        shopsList: [],
        allShopsList: [],
        tags: [],
        id: ''
      }
    },
    methods: {
      init (id) {
        this.shopsList = []
        this.allShopsList = []
        this.visible = true
        this.id = id
        let that = this
        this.$http({
          url: this.getAll.url,
          method: 'get',
          params: this.getAll.data(this)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.allShopsList = data.list
            this.$http({
              url: this.getBinding.url,
              method: 'get',
              params: this.getBinding.data(this)
            }).then(({data}) => {
              if (data && data.code === 0) {
                data.list.forEach(function (item) {
                  that.shopsList.push(that.getBinding.getId(item))
                })
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$http({
          url: this.save.url,
          method: 'post',
          data: this.save.data(this)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.visible = false
            this.$emit('refreshDataList')
          }
        })
      },
      filterMethod (query, item) {
        return item.name.indexOf(query) > -1
      }
    },
    props: {
      transferProps: {
        type: Object,
        default: () =>{
          return {
            key: 'id',
            label: 'name'
          }
        }
      },
      title: {
       type: String,
       default: '店铺关联'
      },
      getAll: {
        type: Object,
        default:() =>{
          return {
            url: `/mall/shops/queryAll`,
            data: (target)=>{ return {level: 2}}
          }
        }
      },
      getBinding: {
        type: Object,
        default: () =>{
          return {
            url: `/mall/shopsgoods/getGoodsShops`,
            data: (target)=>{ return {goodsId: target.id}},
            getId: (target)=>target.shopsId
          }
        }
      },
      save:{
        type: Object,
        default: () =>{
          return {
            url: `/mall/shopsgoods/saveCardShops`,
            data: (target)=>{
              return {
                'goodsId': target.id,
                'shopsIds': target.shopsList
              }
            }
          }
        }
      }
    },
  }
</script>
